package com.by.config;

import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration//声明为配置类
@PropertySource("classpath:db.properties")//读取配置文件
@EnableTransactionManagement//启动事务注解的支持
@ComponentScan("com.by")//扫描包下所有类注入ioc容器
@MapperScan("com.by.mapper")//扫描包下的类 声明为mapper
@EnableWebMvc//启用springmvc注解支持
public class DbConfig {
    @Value("${user}")
    String username;
    @Value("${password}")
    String password;
    @Value("${driver}")
    String driver;
    @Value("${url}")
    String url;
    @Bean//将方法返回值返回ioc容器
    public DataSource dataSource(){
        //配置数据库连接池
        DriverManagerDataSource dataSource=new DriverManagerDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setDriverClassName(driver);
        return dataSource;
    }

    /**
     * 配置事务管理器
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    /**
     * 配置数据库连接工厂
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource){
        SqlSessionFactoryBean factoryBean=new SqlSessionFactoryBean();
        //设置数据源
        factoryBean.setDataSource(dataSource);
        //设置别名
        factoryBean.setTypeAliasesPackage("com.by.bean");
        //mybatis配置类
        org.apache.ibatis.session.Configuration cfg=new org.apache.ibatis.session.Configuration();
        //设定控制台输出sql语句
        cfg.setLogImpl(StdOutImpl.class);
        factoryBean.setConfiguration(cfg);

        //设置映射文件的位置
        PathMatchingResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
        //读取类路径下mybatis文件夹下以Mapper.xml结尾的文件
        Resource [] resources=null;
        try {
            resources=resolver.getResources("classpath:mybatis/*Mapper.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        factoryBean.setMapperLocations(resources);
        try {
            return factoryBean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
//    //配置文件解析器(文件上传时必须存在)
//    @Bean
//    public MultipartResolver multipartResolver(){
//        return new StandardServletMultipartResolver();
//    }
    /**
     * 向容器注入文件上传解析器
     * @return
     */
    @Bean
    public StandardServletMultipartResolver multipartResolver(){
        return new StandardServletMultipartResolver();
    }
}
